import {
  CallHandler,
  ExecutionContext,
  Injectable,
  NestInterceptor,
} from "@nestjs/common";
import { FastifyRequest } from "fastify";
import { Observable, tap } from "rxjs";

@Injectable()
export class TimeInterceptor implements NestInterceptor {
  intercept(
    context: ExecutionContext,
    next: CallHandler<any>,
  ): Observable<any> | Promise<Observable<any>> {
    // console.log();
    const now = Date.now();
    const request: FastifyRequest = context.switchToHttp().getRequest();
    // console.info("start", request.url);

    return next
      .handle()
      .pipe(tap(() => console.info(`${request.url} —— ${Date.now() - now}ms`)));
  }
}
